// --------------------------------------------------------------------------------------------------------------------
// <copyright file="YIELDVWAP.cs" company="">
//   
// </copyright>
// <summary>
//   The yieldvwap.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using Systemathics.FrameWork;

namespace Systemathics.Indicators
{
    [Serializable]
    public sealed class YIELDVWAP : Indicator
    {
        public YIELDVWAP(Instrument instr, Data dt, BarType bt, int value) : base(instr, dt, bt, value)
        {
            Instrument = instr;
        }

        protected override void Calculate(object price)
        {
            if (price is Bar)
            {
                var bar = price as Bar;
                Calculate(bar.BeginTime, bar.Median);
            }
            else if (price is Quote)
            {
                var quote = price as Quote;
                Calculate(quote.Time, quote.Mid);
            }
            else
            {
                var trade = price as Trade;
                Calculate(trade.Time, trade.Price);
            }
        }

        private void Calculate(DateTime time, Decimal value)
        {
            if (Instrument.MarketData.VWAP.HasValue)
            {
                Add(time, (value/Instrument.MarketData.VWAP.Value) - 1);
                IndicatorCalculated(this);
            }
        }
    }
}